home *** CD-ROM | disk | FTP | other *** search
- unit Dirlocku;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables;
-
- type
- TForm1 = class(TForm)
- DataSource1: TDataSource;
- Table1: TTable;
- DBGrid1: TDBGrid;
- TblReadOnlyChk: TCheckBox;
- TblActiveChk: TCheckBox;
- DirLockChk: TCheckBox;
- procedure TblActiveChkClick(Sender: TObject);
- procedure TblReadOnlyChkClick(Sender: TObject);
- procedure DirLockChkClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- uses
- DbiProcs, DbiTypes;
-
- {$R *.DFM}
-
- { Local share must be on in BDE Config for this to work }
- { To be compatible with Delphi 2 which can have databases }
- { open and defined in multiple sessions, the session must }
- { be passed along. In Delphi 1, this is simply the Session }
- { variable. In Delphi 2 it is the dataset's DBSession property }
- procedure DirectoryLock(const DatabaseName: String;
- Session: TSession; LockDir: Boolean);
- const
- DirectoryReadOnly = 'Paradox.DRO';
- LockOrRel: array[Boolean] of function(hDb: hDBIDb; pszTblNam,
- pszDrvType: PChar): DBIResult {$ifdef Win32}stdcall{$endif} =
- (DbiRelPersistTableLock, DbiAcqPersistTableLock);
- begin
- with Session, OpenDatabase(DatabaseName) do
- try
- Check(LockOrRel[LockDir](Handle, DirectoryReadOnly, szParadox));
- finally
- CloseDatabase(FindDatabase(DatabaseName));
- end;
- end;
-
- procedure TForm1.DirLockChkClick(Sender: TObject);
- begin
- DirectoryLock(Table1.DatabaseName,
- {$ifdef Win32}
- Table1.DBSession,
- {$else}
- Session,
- {$endif}
- DirLockChk.Checked);
- end;
-
- procedure TForm1.TblActiveChkClick(Sender: TObject);
- begin
- try
- Table1.Active := TblActiveChk.Checked;
- except
- { If table can't be opened or closed, reset checkbox }
- TblActiveChk.Checked := Table1.Active;
- raise;
- end;
- end;
-
- procedure TForm1.TblReadOnlyChkClick(Sender: TObject);
- begin
- try
- Table1.Readonly := TblReadOnlyChk.Checked;
- except
- { If table can't be made read-only or read-write, reset checkbox }
- TblReadOnlyChk.Checked := Table1.ReadOnly;
- raise;
- end;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- var
- Cfg: SysConfig;
- begin
- { If table is open when directory lock is applied, you }
- { get an unpleasant system error message. This stops it }
- Tag := SetErrorMode(sem_NoOpenFileErrorBox or sem_FailCriticalErrors);
- { Initialise BDE in Delphi 2, before }
- { using IDAPI. Delphi 1 does this for you }
- {$ifdef Win32}
- Session.Open;
- {$endif}
- { Check for local share. If not on, raise exception }
- Check(DbiGetSysConfig(Cfg));
- if not Cfg.bLocalShare then
- raise EDatabaseError.Create(
- 'Local share must be on for successful directory locking');
- { Ensure directory is unlocked. The code assumes this is possible }
- { If the database is on a CD, then it will not be possible }
- try
- DirectoryLock(Table1.DatabaseName, Session, False);
- except
- { Doesn't matter if the directory wasn't locked }
- end;
- DirLockChk.Checked := False;
- TblActiveChk.Checked := Table1.Active;
- TblReadOnlyChk.Checked := Table1.ReadOnly;
- end;
-
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- SetErrorMode(Tag);
- end;
-
- end.
-